package web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.NoticeDao;
import dao.NoticeDaoFactory;
import entity.Notice;

/*
 * 作用：修改通告的具体内容
 */
@WebServlet("/UpdateNoticeServlet")
public class UpdateNoticeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获取客户端发送过来参数
		
		String id = request.getParameter("id");
		String title = request.getParameter("title");
		String author = request.getParameter("author");
		String notice_status_str = request.getParameter("notice_status");
		int notice_status = Integer.parseInt(notice_status_str);
		String content = request.getParameter("content");
		
		
		//2.调用service(dao)来取得结果
		NoticeDao dao = new NoticeDaoFactory().factory();
		//notice实例，这里不应该是new出来，
		//应该先从数据库中取一个最新的数据出来
		Notice notice = dao.queryById(id);
		//此时此刻，这里的notice实例里面的值是跟数据库保持一致
		notice.setTitle(title);
		notice.setAuthor(author);
		notice.setNotice_status(notice_status);
		notice.setContent(content);
		
		//思考一个问题：你是否会关心dao层，更新哪里字段？
		int flag = dao.updateNotice(notice);
		
		//3.根据返回结果，跳转到具体的页面
		if(flag==1){
			response.sendRedirect("success.jsp");
		}else{
			response.sendRedirect("error2.jsp");
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
